﻿using MobiSms.DataModel;
using MobiSms.Library;
using MobiSms.Library.Extensions;
using MobiSms.WebUtility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using MobiSms.Enums;

namespace MobiSms.ViewModel
{
    public class SerialCollectionViewModel : MainViewModel
    {
        #region Constructor
        private int _index = 0;

        public SerialCollectionViewModel()
        {
            SoapClient.BalanceCompleted -= SoapClient_BalanceCompleted;
            SoapClient.BalanceCompleted += SoapClient_BalanceCompleted;

        } 
        #endregion

        #region Properties
        private SerialInfo _selectedSerial;
        public SerialInfo SelectedSerial
        {
            get { return _selectedSerial; }
            set
            {
                _selectedSerial = value;
                RaisePropertyChanged(() => SelectedSerial);
                RaisePropertyChanged(() => DeleteCommand);
            }
        }

        private SerialInfo _currentSerial;

        private bool _processing = false;
        public bool Processing
        {
            get { return _processing; }
            set
            {
                _processing = value;
                RaisePropertyChanged(() => Processing);
            }
        }
        #endregion

        #region Methods
        private void SoapClient_BalanceCompleted(object sender, BalanceCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                if (e.Result.code == 0)
                {
                    _currentSerial.Balance = Convert.ToInt64(e.Result.balance);
                }
                else
                {
                    ShowResult<ResultCodeValue>(e.Result.code);
                    //MessageBox.Show(e.Result.code.ToString());
                }
            }
            else
            {
                MessageBox.Show(e.Error.Message);
            }
            _index++;
            QueryBalance();
        }

        public void QueryBalance()
        {
            if (ItemsOfSerial != null && _index < ItemsOfSerial.Count)
            {
                Processing = true;
                _currentSerial = ItemsOfSerial[_index];
                SoapClient.BalanceAsync(_currentSerial.SerialNumber, _currentSerial.Password);
            }
            else
            {
                Processing = false;
                _index = 0;
            }
        }

        private void Remove(SerialInfo entity)
        {
            Database.CollectionOfSerialNumbers.DeleteOnSubmit(entity);
            Submit();

        }
        #endregion

        #region Commands
        public ICommand BalanceCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return !Processing && ItemsOfSerial != null && ItemsOfSerial.Count > 0; },
                    ExecuteCallback = delegate
                    {
                        Processing = true;
                        QueryBalance();
                    }
                };
            }
        }

        public ICommand DeleteCommand
        {
            get
            {
                return new GenericCommand()
                {
                    CanExecuteCallback = delegate { return SelectedSerial != null; },
                    ExecuteCallback = delegate {
                        if (MessageBox.Show(Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_DELETE_COMFIRM_BODY), Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_DELETE_COMFIRM_TITLE), MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            Remove(SelectedSerial);
                        }
                        
                    }
                };
            }
        }
        #endregion
    }
}
